package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.pojo.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户信息表(User)表控制层
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@RestController
@RequestMapping("user")
@Api(tags = "User信息请求类")
public class UserController {
    /**
     * 服务对象 
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param user 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("getPage")
    public ResponseData getPage(Integer currentPage, Integer pageSize, User user) {
        if (currentPage == null || pageSize == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.getByPage(currentPage, pageSize, user);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据ID查询信息")
    @GetMapping("findById")
    public ResponseData findById(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.findById(id);
    }

    /**
     * 新增数据
     *
     * @param  user 实体
     * @return 新增结果
     */
    @ApiOperation("增加")
    @PostMapping("add")
    public ResponseData add(User user) {
        if(user == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.add(user);
    }

    /**
     * 更新数据
     *
     * @param user 实体
     * @return 更新结果
     */
    @ApiOperation("更新")
    @PostMapping("update")
    public ResponseData update(User user) {
        if(user == null || user.getId() == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.update(user);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    @ApiOperation("删除")
    @PostMapping("delete")
    public ResponseData delete(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("deleteBatch")
    public ResponseData deleteBatch(String ids){
        List<Long> idList = JSONUtil.toList(ids, Long.class);
        if (idList == null || idList.size()<=0){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.deleteBatch(idList);
    }

    /**
     * 绑定微信openid
     * @param code
     * @param request
     * @return
     */
    @ApiOperation("绑定微信openid")
    @PostMapping("bindAccountToWX")
    public ResponseData bindAccountToWX(String code, HttpServletRequest request){
        return userService.bindAccountToWX(code, request);
    }

    /**
     * 根据机构编码和名字查询用户
     */
    @ApiOperation("根据机构编码和名字查询用户")
    @GetMapping("findByEntAndName")
    public ResponseData findByEntAndName(String entCode, String name){
        return userService.findByEntAndName(entCode, name);
    }

    /**
     * 根据id查询用户的前端显示数据
     */
    @ApiOperation("根据id查询用户的前端显示数据")
    @GetMapping("findTableUserById")
    public ResponseData findTableUserById(Long id){
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.findTableUserById(id);
    }

    /**
     * 统计不同角色用户数
     */
    @ApiOperation("统计不同角色用户数")
    @GetMapping("countRole")
    public ResponseData countRole(){
        return userService.countRole();
    }
}

